# -*- coding:utf-8 -*-
import sys

import yaml

from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent.parent))

from handle.handle_path import CASE_FOR_YAML_DIR, API_CASE_FOR_YAML_DIR, UI_CASEDIR, API_CASEDIR # 导入元素配置路径


def ui_yaml_handle(fielname=None):
    """
    处理登录的yaml
    :return:
    """
    if fielname == "login.yaml":
        with open(UI_CASEDIR + "/" + f"{fielname}", "r", encoding='utf-8') as ly:
            ele = yaml.safe_load(ly)
            return ele
    else:
        with open(CASE_FOR_YAML_DIR + "/" + f"{fielname}", "r", encoding='utf-8') as ly:
            ele = yaml.safe_load(ly)
            return ele


def api_yaml_handle(fielname):
    """
    处理登录的yaml
    :return:
    """
    if fielname == "case1.yaml":
        with open(API_CASEDIR + "/" + f"{fielname}", "r", encoding='utf-8') as ly:
            ele = yaml.safe_load(ly)
            return ele
    else:
        with open(API_CASE_FOR_YAML_DIR + "/" + f"{fielname}", "r", encoding='utf-8') as ly:
            ele = yaml.safe_load(ly)
            return ele



if __name__ == "__main__":
    # import os
    # lis = os.listdir(API_CASE_FOR_YAML_DIR)
    # for i in lis:
    #     print(api_yaml_handle(i))
    data = ui_yaml_handle("case1.yaml")
    steps = []
    for key in data['case'].keys():
        if key.startswith('step'):
            steps.append(key)

    # 按step后的数字排序
    sorted_steps = sorted(steps, key=lambda x: int(x[4:]))

    # 获取排序后的step值
    ordered_steps = []
    for step in sorted_steps:
        ordered_steps.append(data['case'][step])
    print(ordered_steps)


